package com.ibeetl.admin.aod.service;

import java.util.List;

import com.ibeetl.admin.aod.util.ToolUtil;
import com.ibeetl.admin.aod.web.query.AodIndustryQuery;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ibeetl.admin.core.util.PlatformException;

import com.ibeetl.admin.aod.dao.AodIndustryDao;
import com.ibeetl.admin.aod.entity.AodIndustry;
import com.ibeetl.admin.core.service.BaseService;

/**
 * AodIndustry Service
 */

@Service
@Transactional
public class AodIndustryService extends BaseService<AodIndustry>{

    @Autowired
    @Qualifier("defaultJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Autowired
    protected AodIndustryDao aodIndustryDao;

    @Autowired
    protected Environment env;


    public PageQuery<AodIndustry>queryByCondition(PageQuery query){
        PageQuery ret =  aodIndustryDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public void batchDelAodIndustry(List<Long> ids){
        try {
            if (CollectionUtils.isNotEmpty(ids)) {
                for (Long id : ids) {
                    AodIndustry aodIndustry = aodIndustryDao.unique(id);
                    dynamicDropTable(aodIndustry);
                }
            }
            aodIndustryDao.batchDelAodIndustryByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除行业类别失败", e);
        }
    }

    public void saveUnique(AodIndustry aodIndustry) {
        AodIndustry existAodIndustry = aodIndustryDao.queryByName(aodIndustry.getIndustryName());
        if (null != existAodIndustry) {
            throw new PlatformException(String.format("行业类别名称：%s已存在,添加失败", aodIndustry.getIndustryName()));
        }

        existAodIndustry = aodIndustryDao.queryByTableName(aodIndustry.getIndustryTable());
        if (null != existAodIndustry) {
            throw new PlatformException(String.format("行业类别表名：%s已存在,添加失败", aodIndustry.getIndustryTable()));
        }

        try {
            dynamicCreateTable(aodIndustry);

            aodIndustryDao.insert(aodIndustry);
        } catch (Exception e) {

            dynamicDropTable(aodIndustry);

            throw e;
        }

    }

    public void dynamicDropTable(AodIndustry aodIndustry) {

        String tableName = aodIndustry.getIndustryTable();

        // 删除行业类别 必须删除表
        String dropTableSql = String.format(env.getProperty("aod.dropTableSql"), tableName);

        jdbcTemplate.execute(dropTableSql);

        /*String dropIndexSql = env.getProperty("aod.dropIndexSql");
        String[] sqlArr = dropIndexSql.split(";");
        for (String sql : sqlArr) {
            dynamicDropIndex(sql, tableName);
        }*/

    }

    /*public void dynamicDropIndex(String sql, String tableName) {

        try {
            String dropIndexSql = String.format(sql, tableName);

            jdbcTemplate.execute(dropIndexSql);

        } catch (Exception e) {
            logger.error("动态删除索引失败", e);
        }

    }*/

    public void dynamicCreateTable(AodIndustry aodIndustry) {

        String tableName = aodIndustry.getIndustryTable();

        String createTableSql = String.format(env.getProperty("aod.createTableSql"), tableName);

        jdbcTemplate.execute(createTableSql);

        String createIndexSql = env.getProperty("aod.createIndexSql");
        String[] sqlArr = createIndexSql.split(";");
        for (String sql : sqlArr) {
            dynamicCreateIndex(sql, tableName);
        }

    }

    public void dynamicCreateIndex(String sql, String tableName) {

        try {
            String createIndexSql = String.format(sql, tableName, tableName);

            jdbcTemplate.execute(createIndexSql);
        } catch (Exception e) {
            logger.error("动态创建索引失败", e);
        }

    }

    public void updateUnique(AodIndustry aodIndustry) {
        AodIndustry existAodIndustry = aodIndustryDao.queryByNameAndNonId(aodIndustry.getIndustryName(),aodIndustry.getId());
        if (null != existAodIndustry) {
            throw new PlatformException(String.format("行业类别名称：%s已存在,修改失败", aodIndustry.getIndustryName()));
        }

        existAodIndustry = aodIndustryDao.queryByTableNameAndNonId(aodIndustry.getIndustryTable(),aodIndustry.getId());
        if (null != existAodIndustry) {
            throw new PlatformException(String.format("行业类别表名：%s已存在,修改失败", aodIndustry.getIndustryTable()));
        }

        AodIndustry oldAodIndustry = aodIndustryDao.unique(aodIndustry.getId());

        dynamicDropTable(oldAodIndustry);

        aodIndustryDao.updateById(aodIndustry);

        dynamicCreateTable(aodIndustry);

    }

    public List<AodIndustry> queryList(AodIndustryQuery aodIndustryQuery) {
        return aodIndustryDao.queryList(aodIndustryQuery);
    }

}